package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.cmp;
import static net.sf.dynamicreports.report.builder.DynamicReports.field;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.awt.Color;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.chart.MeterChartBuilder;
import net.sf.dynamicreports.report.constant.MeterShape;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class MeterChart extends MutiReport {

	@At("/getMeterChart")
	@Ok("json")
	public String getMeterChart(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("getMeterChart");
		build(request, response);
		//System.out.println("dddddddd : " + resultResult);
		return resultResult;
	}

	protected void build(HttpServletRequest request,
			HttpServletResponse response) {
		MeterChartBuilder chart1 = cht.meterChart()
			.setValue(field("value", type.integerType()))
			.setShape(MeterShape.DIAL);

		MeterChartBuilder chart2 = cht.meterChart()
			.setValue(18)
			.setDataRangeHighExpression(30)
			.setTickInterval(5d)
			.setTickColor(Color.BLACK)
			.setNeedleColor(Color.BLACK)
			.setValueColor(Color.BLACK)
			.setMeterBackgroundColor(Color.LIGHT_GRAY)
			.intervals(
				cht.meterInterval()
					.setLabel("Normal")
					.setBackgroundColor(new Color(150, 255, 150))
					.setDataRangeLowExpression(0)
					.setDataRangeHighExpression(20),
				cht.meterInterval()
					.setLabel("Warning")
					.setBackgroundColor(new Color(255, 255, 150))
					.setDataRangeLowExpression(20)
					.setDataRangeHighExpression(25),
				cht.meterInterval()
					.setLabel("Critical")
					.setBackgroundColor(new Color(255, 150, 150))
					.setDataRangeLowExpression(25)
					.setDataRangeHighExpression(30));

		try {
			reportBuilder = report()
			  .setTemplate(Templates.reportTemplate)
			  .title(Templates.createTitleComponent(""))
			  .summary(
			  	cmp.horizontalList(chart1, chart2))
			  .pageFooter(Templates.footerComponent)
			  .setDataSource(createDataSource());
			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}

	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("value");
		dataSource.add(40);
		return dataSource;
	}
}
